package com.china.school.proxyfactory;

import com.china.school.pojo.User;
import org.aopalliance.aop.Advice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.StaticMethodMatcherPointcut;

import java.lang.reflect.Method;

public class ProxyFactoryTest {
    public static void main(String[] args) {
        //        ProxyCreatorSupport有3个子类
        //上面我们介绍了两种动态代理技术，那么在Spring中进行了封装，封装出来的类叫做ProxyFactory，表示是创建代理对象的一个工厂，使用起来会比上面的更加方便，比如：

        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvisor(new PointcutAdvisor(){
            @Override
            public Pointcut getPointcut() {
                return new StaticMethodMatcherPointcut() {
                    @Override
                    public boolean matches(Method method, Class<?> targetClass) {
                        if (method.getName().contains("eat")){
                            return true;
                        }
                        return false;
                    }
                };
            }

            @Override
            public Advice getAdvice() {
                return new MethodBeforeAdvice() {
                    @Override
                    public void before(Method method, Object[] args, Object target) throws Throwable {
                        System.out.println("--------before----------");
                    }
                };
            }

            @Override
            public boolean isPerInstance() {
                return false;
            }


        });

        User proxy = (User)proxyFactory.getProxy();
        proxy.eat();
    }
}
